package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    //查询所有课程信息列表
    @Override
    public PageResult<Course> list(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        //1.设置pageHelper分页查询
        PageHelper.startPage(page,pageSize);
        //2.调用mapper接口方法 执行查询
        List<Course> courselist = courseMapper.selectList(name,subject,target);
        //3.将查询结果封装成PageResult对象并返回
        Page<Course> p = (Page<Course>)courselist;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    //删除课程信息
    @Override
    public void delete(Integer id) {
        courseMapper.deleteById(id);

    }
    //添加课程信息
    @Override
    public void add(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.insert(course);

    }
    //根据ID查询课程信息
    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }
    //修改课程信息

    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById(course);

    }
    //查询所有课程信息
    @Override
    public List<Course> listAll() {
        return courseMapper.selectAll();
    }
    //根据课程类别查询课程信息

    @Override
    public List<Course> listBySubject(Integer subject) {
        return courseMapper.listBySubject(subject);
    }
}
